package qq2564874169.tuaotuao.fx.orm.where;


import qq2564874169.tuaotuao.fx.orm.DbEntity;
import qq2564874169.tuaotuao.fx.orm.SqlCore;
import qq2564874169.tuaotuao.fx.orm.SqlParam;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseWhereParser {
    private SqlCore core;

    public BaseWhereParser(SqlCore core) {
        this.core = core;
    }

    protected String escape(String name) {
        return core.escapeField(name);
    }

    protected String paramName(String name) {
        return core.paramName(name);
    }

    protected List<String> parseIn(boolean isIn, SqlParam param, Class<?> table, List<?> items, DbEntity entity) {
        List<String> list = new ArrayList<>();
        Map<String, List> map = new LinkedHashMap<>();
        for (Object item : items) {
            Map<String, Object> data = entity.getValues(item);
            for (String name : data.keySet()) {
                if (map.containsKey(name) == false) {
                    map.put(name, new ArrayList<>());
                }
                map.get(name).add(data.get(name));
            }
        }
        for (String name : map.keySet()) {
            String pn = "in_" + name;
            param.args.put(pn, map.get(name));
            if (isIn) {
                list.add(escape(name) + " in " + paramName(pn));
            } else {
                list.add(escape(name) + " not in " + paramName(pn));
            }
        }
        return list;
    }
}
